package com.trkj.controller.wdm;

import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.trkj.pojo.CharacteristicItem;
import com.trkj.pojo.DefinitionDept;
import com.trkj.pojo.Duty;
import com.trkj.pojo.Edubackground;
import com.trkj.pojo.JobStatus;
import com.trkj.pojo.MaritalStatus;
import com.trkj.pojo.MotherTongue;
import com.trkj.pojo.Nationality;
import com.trkj.pojo.ProfessionalTitle;
import com.trkj.pojo.SocialRelation;
import com.trkj.pojo.StaffInformation;
import com.trkj.service.ServiceException;
import com.trkj.service.wdm.StaffInformationService;
import com.trkj.service.wdm.StaffService;
import com.trkj.vo.Result2;
import com.trkj.vo.ShouYeVo;
import com.trkj.vo.StaffInformationVO;
import com.trkj.vo.StaffMessage;
import com.trkj.vo.StaffVO;

@Controller
public class StaffController {
	@Autowired
	private StaffInformationService staffinformationservice;
	@Autowired
	private StaffService staffservice;
	
	
	
	
	
	
	
	
	
	
	
	//员工信息分页查询
	@PostMapping("queryByto_db_staffinformation")
	@ResponseBody
	public Map queryByto_db_staffinformation(int page, int rows) {
		long total = staffinformationservice.queryByPageCount();
		List<StaffInformationVO> list = staffinformationservice.queryByPage(page, rows);
		Map map = new HashMap<>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	
	
	//员工信息
	@GetMapping("to_staff")
	public String to_staff(){
		return "empmessage_yj";
	}
	//新增员工信息
	@RequestMapping("empmessage_insert")
	public String toempmessageinsert(){
		return "empmessage_insert_yj";
	}
	//修改员工信息
	@RequestMapping("empmessage_update")
	public String toempmessageupdate(Long id,Model m){
		StaffInformation s = staffservice.findOneEmp(id);
		m.addAttribute("s", s);
		return "empmessage_update_yj";
	}
	
	
	
	/**
	 * 图片上传
	 * @param f
	 * @param request
	 * @return
	 * @throws IllegalStateException
	 * @throws IOException
	 */
	@PostMapping("upload")
	@ResponseBody
	public Map upload(@RequestParam("f") CommonsMultipartFile f, 
			HttpServletRequest request) throws IllegalStateException, IOException{
		//将上传的文件保存到工程目录的uploads子目录下
		ServletContext application = request.getServletContext();
		//获取工程所在的物理路径，并添加子目录uploads
		String path = application.getRealPath("")+""; 
	    //获取工程所在的images路劲，并添加目录uploads 
	    path = path+"\\WEB-INF\\images\\uploads"; 
		//创建uploads文件夹
		File dir = new File(path);
		if(!dir.exists()) dir.mkdirs();
		//获取原始文件的扩展名
		String originalFileName = f.getOriginalFilename();
		int pos = originalFileName.lastIndexOf('.');//a.b.c.jpg
		String ext = originalFileName.substring(pos);//获取到扩展名.jpg
		//生成唯一的新文件名
		String newFileName = UUID.randomUUID().toString().toUpperCase() + ext;
		//转存
		f.transferTo(new File(dir, newFileName));//将文件转存到指定的位置
		Map map = new HashMap();
		map.put("file", "/hr/images/uploads/"+newFileName);
		return map;
	}
	/**
	 * 员工信息新增
	 * @param s
	 * @return
	 * @throws ServiceException
	 */
	@RequestMapping("addStaff_yj")
	@ResponseBody
	public Result2 addStaff(StaffMessage s) throws ServiceException{
		try {
			staffservice.addStaff(s);
			return Result2.SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result2.failure(e.getMessage());
		}
		
	}
	/**
	 * 员工信息编辑
	 * @param s
	 * @return
	 * @throws ServiceException
	 */
	@RequestMapping("update_yj")
	@ResponseBody
	public Result2 updateStaff(StaffMessage s,HttpSession session) throws ServiceException{
		try {
			staffservice.updateStaff(s);
			
			StaffInformation saff = (StaffInformation) session.getAttribute("staff");
			
			
			if(s.getStain_id().longValue()== saff.getStainId()){
				StaffInformation sss = staffservice.findOneEmp(saff.getStainId());
				List<ShouYeVo> syvo =staffservice.querysy(saff.getStainId());
				
				session.setAttribute("staff", sss);
				session.setAttribute("sy", syvo);
				
			}
			
			
			
			return Result2.SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result2.failure(e.getMessage());
		}
		
	}
	
	/**
	 * 查询所有特征项
	 * @return
	 */
	@RequestMapping("find_all_char")
	@ResponseBody
	public List<CharacteristicItem> findAllChar(){
		return staffinformationservice.AllChar();
	}
	/**
	 * 查询所有部门
	 * @return
	 */
	@RequestMapping("find_all_dept")
	@ResponseBody
	public List<DefinitionDept> findAllDept(){
		return staffinformationservice.AllDept();
	}
	/**
	 * 查询所有职务
	 * @return
	 */
	@RequestMapping("find_all_duty")
	@ResponseBody
	public List<Duty> findAllDuty(){
		return staffinformationservice.AllDuty();
	}
	
	/**
	 * 查询所有用工状态
	 */
	@RequestMapping("find_all_jobstatus")
	@ResponseBody
	public List<JobStatus> AllJobStatus(){
		return staffinformationservice.AllJobStatus();
	}
	
	/**
	 * 查询所有学历
	 */
	@RequestMapping("find_all_edubackground")
	@ResponseBody
	public List<Edubackground> AllEdubackground(){
		return  staffinformationservice.AllEdubackground();
	}
	
	
	/**
	 * 查询所有婚姻状况
	 */
	@RequestMapping("find_all_maritalStatus")
	@ResponseBody
	public List<MaritalStatus> AllMaritalStatus(){
		return staffinformationservice.AllMaritalStatus();
	}
	
	/**
	 * 查询所有民族
	 */
	@RequestMapping("find_all_nationality")
	@ResponseBody
	public List<Nationality> AllNationality(){
		return  staffinformationservice.AllNationality();
	}
	

	/**
	 * 查询所有母语
	 * @return
	 */
	@RequestMapping("find_all_motherTongue")
	@ResponseBody
	public List<MotherTongue> AllMotherTongue(){
		return staffinformationservice.AllMotherTongue();
	}
	

	/**
	 * 查询所有社会关系
	 * @return
	 */
	@RequestMapping("find_all_socialRelation")
	@ResponseBody
	public List<SocialRelation> AllSocialRelation(){
		return staffinformationservice.AllSocialRelation();
	}
	
	/**
	 * 查询所有职称
	 */
	@RequestMapping("find_all_professionalTitle")
	@ResponseBody
	public List<ProfessionalTitle> AllProfessionalTitle(){
		return staffinformationservice.AllProfessionalTitle();
	}
	/**
	 * 分页查询
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("find-staff-yj")
	@ResponseBody
	public Map findAll(int page,int rows){
		long total = staffservice.findCount();
		List<StaffInformation> list = staffservice.findAll(page, rows);
		Map map = new HashMap<>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	
	
	/**
	 * 分页查询
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("find-like-staff-yj")
	@ResponseBody
	public Map findLikeAll(String name,int page,int rows){
		long total = staffservice.findLikeCount(name);
		List<StaffInformation> list = staffservice.findLikeAll(name,page, rows);
		Map map = new HashMap<>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	/**
	 * 增加员工离职信息
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("delete-emp-labor-yj")
	@ResponseBody
	public Result2 insertLabor(Long id,Long id2,Timestamp time,String person,String beizhu){
		try {
			staffservice.insertLaborTurnover(id, id2,time,person,beizhu);
			return Result2.SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result2.failure(e.getMessage());
		}
	}
	
	/**
	 * 员工离职信息回显
	 * @param id
	 * @param m
	 * @return
	 */
	@RequestMapping("empmessage_delete")
	public String toempmessagedelete(Long id,Model m){
		StaffInformation s = new StaffInformation();
		if(id!=null){
			s = staffservice.findOneEmp(id);
		}
		m.addAttribute("s_delete", s);
		return "empmessage_delete_yj";
	}
	//员工离职
	@GetMapping("to_LaborTurnover")
	public String to_LaborTurnover(){
		return "laborturnover_yj";
	}
	
	
	/**
	 * 员工离职分页查询
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("find-laborturnover-yj")
	@ResponseBody
	public Map findLaborTurnoverAll(int page,int rows){
		Page<Object> p = PageHelper.startPage(page,rows);
		staffservice.laborFindAll();
		Map map = new HashMap<>();
		map.put("total", p.getTotal());
		map.put("rows", p.getResult());
		return map;
	}

	/**
	 * 员工离职分页查询
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("find-like-laborturnover-yj")
	@ResponseBody
	public Map findLikeLaborTurnoverAll(String name,int page,int rows){
		Page<Object> p = PageHelper.startPage(page,rows);
		staffservice.laborLikeFindAll(name);
		Map map = new HashMap<>();
		map.put("total", p.getTotal());
		map.put("rows", p.getResult());
		return map;
	}
	
	
	
	
	//员工离职新增
	@GetMapping("to_LaborTurnover_insert")
	public String to_LaborTurnoverinsert(){
		return "laborturnover_insert_yj";
	}
	
	
	//员工离职修改
	@GetMapping("to_LaborTurnover_update")
	public String to_LaborTurnoverupdate(){
		return "laborturnover_update_yj";
	}
	
	
	//返聘
	@RequestMapping("dis")
	@ResponseBody
	public Map dis(int page,int rows){
		Page<Object> p = PageHelper.startPage(page,rows);
		staffservice.foldBackFindAll();
		Map map = new HashMap<>();
		map.put("total", p.getTotal());
		map.put("rows", p.getResult());
		return map;
	}
	//返聘
		@RequestMapping("dis_select")
		@ResponseBody
		public Map dis_select(String name,int page,int rows){
			Page<Object> p = PageHelper.startPage(page,rows);
			staffservice.foldLikeBackAll(name);
			Map map = new HashMap<>();
			map.put("total", p.getTotal());
			map.put("rows", p.getResult());
			return map;
		}
	
	
	//返聘改状态
	@RequestMapping("dis_update")
	@ResponseBody
	public Result2 dis_update(Long id){
		try {
			staffservice.updateFoldBack(id);
			return Result2.SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result2.failure(e.getMessage());
		}
	}
	
	
	
	//返聘页面
	@GetMapping("to_dis")
	public String to_dis(){
		return "wdm_dis";
	}
	
}
